package com.itheima.project.web;

import com.itheima.project.basic.ResponseWrap;
import com.itheima.project.core.enums.security.UserEnum;
import com.itheima.project.face.UserAdapterFace;
import com.itheima.project.utils.ResponseWrapBuild;
import com.itheima.project.vo.security.ResourceVo;
import com.itheima.project.vo.security.RoleVo;
import com.itheima.project.vo.security.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName UserAdapterController.java
 * @Description 用户适配controller
 */
@RestController
@RequestMapping("user-adapter")
@Api(tags = "用户适配controller")
@Slf4j
public class UserAdapterController {

    @Autowired
    UserAdapterFace userAdapterFace;

    /**
     * @Description 按用户名查找用户
     * @param username 登录名
     * @return
     */
    @GetMapping("find-user-username-enterprise/{username}/{enterpriseId}")
    @ApiOperation(value = "按用户名查找用户",notes = "按用户名查找用户")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path",name = "username",value = "用户名称",dataType = "String"),
        @ApiImplicitParam(paramType = "path",name = "enterpriseId",value = "企业id",dataType = "Long")
    })
    ResponseWrap<UserVo> findUserByUsernameAndEnterpriseId(@PathVariable("username") String username,
                                                           @PathVariable("enterpriseId") Long enterpriseId){
        UserVo userVo = userAdapterFace.findUserByUsernameAndEnterpriseId(username, enterpriseId);
        return ResponseWrapBuild.build(UserEnum.SUCCEED,userVo);
    }

    /**
     * @Description 按用户openid查找用户
     * @param openId 登录名
     * @return
     */
    @ApiOperation(value = "按用户openid查找用户",notes = "按用户openid查找用户")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path",name = "openId",value = "用户openid",dataType = "String"),
        @ApiImplicitParam(paramType = "path",name = "enterpriseId",value = "企业id",dataType = "Long")
    })
    @GetMapping("find-user-openid-enterprise/{openId}/{enterpriseId}")
    ResponseWrap<UserVo> findUserByOpenIdAndEnterpriseId(@PathVariable("openId") String openId,
                                                         @PathVariable("enterpriseId")Long enterpriseId){
        UserVo userVo = userAdapterFace.findUserByOpenIdAndEnterpriseId(openId, enterpriseId);
        return ResponseWrapBuild.build(UserEnum.SUCCEED,userVo);
    }

    /**
     * @Description 按用户手机查找用户
     * @param mobile 登录名
     * @return
     */
    @GetMapping("find-user-mobile-enterprise/{mobile}/{enterpriseId}")
    @ApiOperation(value = "按用户手机查找用户",notes = "按用户手机查找用户")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path",name = "mobile",value = "用户手机",dataType = "String"),
        @ApiImplicitParam(paramType = "path",name = "enterpriseId",value = "企业id",dataType = "Long")
    })
    ResponseWrap<UserVo> findUserByMobileAndEnterpriseId(@PathVariable("mobile")String mobile,
                                           @PathVariable("enterpriseId")Long enterpriseId){
        UserVo userVo = userAdapterFace.findUserByMobileAndEnterpriseId(mobile, enterpriseId);
        return ResponseWrapBuild.build(UserEnum.SUCCEED,userVo);
    }

    /**
     * @Description 查找用户所有角色
     * @param userId 用户Id
     * @return
     */
    @GetMapping("find-role-user/{userId}")
    @ApiOperation(value = "查找用户所有角色",notes = "查找用户所有角色")
    @ApiImplicitParam(paramType = "path",name = "userId",value = "用户id",dataType = "Long")
    ResponseWrap<List<RoleVo>> findRoleByUserId(@PathVariable("userId")Long userId){
        List<RoleVo> roleVos = userAdapterFace.findRoleByUserId(userId);
        return ResponseWrapBuild.build(UserEnum.SUCCEED,roleVos);
    }

    /**
     * @Description 查询用户有资源
     * @param userId 用户Id
     * @return
     */
    @GetMapping("find-resoure-user/{userId}")
    @ApiOperation(value = "查询用户有资源",notes = "查询用户有资源")
    @ApiImplicitParam(paramType = "path",name = "userId",value = "用户id",dataType = "Long")
    ResponseWrap<List<ResourceVo>> findResourceByUserId(@PathVariable("userId")Long userId){
        List<ResourceVo> resourceVos = userAdapterFace.findResourceByUserId(userId);
        return ResponseWrapBuild.build(UserEnum.SUCCEED,resourceVos);
    }

    /***
     * @description 查询当前用户
     * @param userId
     * @return
     */
    @GetMapping("find-cuttent-user/{userId}")
    @ApiOperation(value = "查询当前用户",notes = "查询当前用户")
    @ApiImplicitParam(paramType = "path",name = "userId",value = "用户id",dataType = "Long")
    ResponseWrap<UserVo> findCurrentUser(@PathVariable("userId")Long userId){
        UserVo userVo = userAdapterFace.findCurrentUser(userId);
        return ResponseWrapBuild.build(UserEnum.SUCCEED,userVo);
    }
}
